<template>
  <div>
    <input v-model="inputQuestion" placeholder="输入问题" />
    <input v-model="userId" placeholder="用户id" />
    <button @click="sendStreamRequest">发送</button>
    <div>回答：{{ answer }}</div>
  </div>
</template>

<script setup>
import { ref } from "vue";

defineProps({
  msg: String,
});

const count = ref(0);
const inputQuestion = ref("");
const userId = ref("");
const answer = ref("");
const sendStreamRequest = async () => {
  try {
    const res = await fetch(
      "http://localhost:9000/ai/streamWithId?question=" +
        inputQuestion.value +
        "&memoryId=" +
        userId.value
    );
    const reader = res.body.getReader();
    const decoder = new TextDecoder("utf-8");
    while (true) {
      const { done, value } = await reader.read();
      if (done) {
        break;
      }
      const chunk = decoder.decode(value);
      answer.value += chunk;
    }
  } catch (err) {
    console.log(err);
  }
};
</script>

<style scoped>
.read-the-docs {
  color: #888;
}
</style>
